$(document).ready(function() {
	useradminset_fileupload();
	adduseradmin_set_fileupload();
    get_hash();
	$("#strUserBirthDayadmin, #addstrUserBirthDayadmin").datepicker({ 
		changeYear: true,
	});	
	$("#submitUseradmin").click(function(){ 
		saveUsersadmin();
	});	
	$("#add_useradmin_form").click(function(){
		addUserFormadmin();
	});
	$("#addsubmitUseradmin").click(function(){
		addUseradmin();
	});
});
function usersadmin(){
	var asort = 0;
	usersadmin_icons = {};
	xfunc = "usersadmin/list_usersadmin";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					usersadmin_icons_panel = '';
						usersadmin_icons_panel +='<tr>';;
							usersadmin_icons_panel +='<td><img width="86" height="66" class="img-rounded" src="'+base_url+'assets/uploaded/user_img/thumbnail/'+result.strImgLink[k]+'" alt=""/></td>';
							usersadmin_icons_panel +='<td>'+result.strName[k]+'</td>';
							usersadmin_icons_panel +='<td>'+result.dateBirthday[k]+'</td>';
							usersadmin_icons_panel +='<td>'+result.intAge[k]+'</td>';
							usersadmin_icons_panel +='<td>'+result.strEmail[k]+'</td>';
							usersadmin_icons_panel +='<td class="controls"><div class="btn-group">';
							usersadmin_icons_panel +='<button class="btn btn-warning" onclick="javascript:changePermissionadmin(\''+result.intID[k]+'\')"><i class="icon-star"></i>Make Client User</button>';
							usersadmin_icons_panel +='<button class="btn btn-primary" onclick="javascript:editUseradmin(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							usersadmin_icons_panel +='<button class="btn btn-danger" onclick="javascript:delUseradmin(\''+result.intID[k]+'\', \''+result.strName[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							usersadmin_icons_panel +='</td>';
						usersadmin_icons_panel +='</tr>';
						usersadmin_icons[result.intID[k]]=usersadmin_icons_panel;
				});
				
				usersadmin_html_panel = '<table class="table table-striped table-bordered" id="usersadmin_table">';
				usersadmin_html_panel += '<thead>';
					usersadmin_html_panel += '<tr>';
						usersadmin_html_panel += '<th class="thumb-photo">Photo</th>';
						usersadmin_html_panel += '<th class="note-content">User Name</th>';
						usersadmin_html_panel += '<th class="note-title">Birthdate</th>';
						usersadmin_html_panel += '<th class="note-title">Age</th>';
						usersadmin_html_panel += '<th class="note-title">E-mail Address</th>';
						usersadmin_html_panel += '<th>Control</th>';
					usersadmin_html_panel += '</tr>';
				usersadmin_html_panel += '</thead>';
				usersadmin_html_panel += '<tbody>';
				
				for(x=0;x<=asort;x++){
					if($.trim(usersadmin_icons[x])!=""){
						usersadmin_html_panel +=usersadmin_icons[x];
					}
								
				}		
				usersadmin_html_panel += '</tbody>';		
				usersadmin_html_panel += '</table>';
				
				$('#usersadmin_content').html(usersadmin_html_panel);
				$('#usersadmin_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}

function addUserFormadmin(){
	$("#usersadmin_content, #add_useradmin_form").hide();
	$("#addUsersadmin").slideDown('slow', function(){
		$("#usersadmin_content").hide();
		$("#addUsersadmin").slideDown('slow', function(){
			$(".back-btn-adduseradmin a").attr("onclick","btn_back_adduseradmin()");
		});	
	});	
}
function addUseradmin(){
	var thumbnailLink = $("#adduseradmin_thumbnailLink").val();
	var addstrUserName = $("#addstrUserNameadmin").val();
	var addstrUserBirthDay = $("#addstrUserBirthDayadmin").val();
	var addstrUserEmail = $("#addstrUserEmailadmin").val();
	var addstrPassword = $("#addstrPasswordadmin").val();

	bDate = addstrUserBirthDay.split("/");
	
	var today = new Date(); 
    var birthDate = new Date(bDate); 
    var age = today.getFullYear() - birthDate.getFullYear(); 
    var m = today.getMonth() - birthDate.getMonth(); 

    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { 
        age--; 
    }
	var ageUser = age;  
	birthDate = bDate[2]+'-'+bDate[0]+'-'+bDate[1];  
	
	if(thumbnailLink==''){
		$("#errorUsersadmin").html("<div class='alert alert-error'>Error: Put the photo of user!</div>");
	} else if(addstrUserName=='') {
		$("#errorUsersadmin").html("<div class='alert alert-error'>Error: Put the name of user!</div>");
	} else if(addstrUserBirthDay=='') {
		$("#errorUsersadmin").html("<div class='alert alert-error'>Error: Put the age of user!</div>");
	} else if(addstrUserEmail=='') {
		$("#errorUsersadmin").html("<div class='alert alert-error'>Error: Put the e-mail!</div>");
	} else {
		xfunc = "usersadmin/add_useradmin";
		data_sent = {
				'strUserName':addstrUserName,
				'strBirthDay':birthDate,
				'ageUser':ageUser,
				'strPassword':addstrPassword,
				'strUserEmail':addstrUserEmail,
				'strImgLink':thumbnailLink
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorUsersadmin").hide();
				$("#addUsersadminSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#thumbnailLinkadmin").val('');
				$("#addstrUserNameadmin").val('');
				$("#addstrUserBirthDayadmin").val('');
				$("#addstrUserEmailadmin").val('');
				$("#addstrUserPointadmin").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editUseradmin(noteID){
	xfunc = "usersadmin/get_useradmin_data";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			bDate = result.dateBirthday.split("-");
			birthDate = bDate[1]+'/'+bDate[2]+'/'+bDate[0];
			var users_thumbnail = '<div class="imgThumbnail"><img width="86" class="img-rounded" src="'+base_url+'assets/uploaded/user_img/thumbnail/'+result.strImgLink+'" alt=""/></div>';
			$("#intUIDadmin").val(result.intID);
			$("#strUserNameadmin").val(result.strName);
			$("#strUserBirthDayadmin").val(birthDate);
			$("#strUserEmailadmin").val(result.strEmail);
			$("#useradmin_thumbnail").html(users_thumbnail);
			$("#useradmin_thumbnailLink").val(result.strImgLink);
		}
	});

	$("#add_useradmin_form").hide();
	$("#usersadmin_content").hide();
	$("#editUsersadmin").slideDown('slow', function(){
		$(".back-btn-useradmin a").attr("onclick","btn_back_useradmin()");
	});	
}

function saveUsersadmin(){
	var intID = $("#intUIDadmin").val();
	var strUserName = $("#strUserNameadmin").val();
	var strBirthDay = $("#strUserBirthDayadmin").val();
	var strUserEmail = $("#strUserEmailadmin").val();
	var strImgLink = $("#useradmin_thumbnailLink").val();
	var strPassword = $("#strPasswordadmin").val();

	bDate = strBirthDay.split("/");
	
	var today = new Date(); 
    var birthDate = new Date(bDate); 
    var age = today.getFullYear() - birthDate.getFullYear(); 
    var m = today.getMonth() - birthDate.getMonth(); 

    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { 
        age--; 
    }
	var ageUser = age;  
	birthDate = bDate[2]+'-'+bDate[0]+'-'+bDate[1];  
	
	xfunc = "usersadmin/save_usersadmin";
	data_sent = {
			'intID':intID,
			'strUserName':strUserName,
			'ageUser':ageUser,
			'strPassword':strPassword,
			'strBirthDay':birthDate,
			'strUserEmail':strUserEmail,
			'strImgLink':strImgLink
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#usersadminSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delUseradmin(noteID, titleName){
	xfunc = "usersadmin/delete_useradmin";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				usersadmin();
			}
		});
    });
}
function changePermissionadmin(intID){
	xfunc = "usersadmin/change_perm_useradmin";
	data_sent = {
			'intID':intID
	};
		
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			usersadmin();
		}
	});
}
function btn_back_adduseradmin(){
	$("#addUsersadmin").hide();
	$("#usersadmin_content").slideDown('fast', function(){
		$("#add_useradmin_form").show();
		usersadmin();
	});	
}
function btn_back_useradmin(){
	$("#editUsersadmin").hide();
	$("#usersadmin_content").slideDown('fast', function(){
		$("#add_useradmin_form").show();
		usersadmin();
	});	
}
function useradminset_fileupload(){
	$('#useradmin_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	useradminfile_upload();
}
function useradminfile_upload(){
	$('.useradmin_fileupload').fileupload();
	$('.useradmin_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+v.name+'" />';
					$("#useradmin_thumbnailLink").val(v.name);			
					$(".imgThumbnail").hide();
					$(".imgThumbnail").slideDown('slow', function(){
						$(".imgThumbnail").html(html);
					});					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}
//ADD DATA
function adduseradmin_set_fileupload(){
	$('#adduseradmin_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	adduseradmin_file_upload();
}
function adduseradmin_file_upload(){
	$('.adduseradmin_fileupload').fileupload();
	$('.adduseradmin_fileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/user_img/thumbnail/"+v.name+'" />';
					$("#adduseradmin_thumbnailLink").val(v.name);			
					$("#adduseradmin_thumbnail").html(html);			
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}